package org.jeecg.modules.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonCuckoo;
import org.jeecg.modules.entity.CuUserDetails;
import org.jeecg.modules.service.ICuUserDetailsService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 清理会员
 *
 * @author: wei
 * @date: 2024年08月08日 17:44
 */
@Slf4j
public class CleanUpMembers implements Job {

    @Autowired
    private ICuUserDetailsService cuUserDetailsService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("清理会员...");
        cleanUpMembers();
    }

    // 清理会员
    public void cleanUpMembers() {
        // 查询用户 条件 会员结束日期 小于当前时间
        QueryWrapper<CuUserDetails> queryWrapper = new QueryWrapper<>();
        queryWrapper.lt("end_vip", new Date());
        queryWrapper.eq("is_vip", CommonCuckoo.IS_VIP);
        List<CuUserDetails> cuUserDetailsList = cuUserDetailsService.list(queryWrapper);
        for (CuUserDetails cuUserDetails : cuUserDetailsList){
            cuUserDetails.setIsVip(CommonCuckoo.IS_NOT_VIP);
            cuUserDetailsService.updateById(cuUserDetails);
        }
    }
}
